﻿using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.WebServices.MediaGalleries;
using System.IO;
using System.Windows.Forms;

namespace nb.development.csmediamanager
{
    public class AttachmentUploader
    {
        public AttachmentUploader(int galleryid, Guid siteid, string url)
        {
            remote = true;
            this.galleryid = galleryid;
            this.url = url;
            this.siteid = siteid;
        }

        public AttachmentUploader(int galleryid, Guid siteid, Stream filestream, string filename)
        {
            remote = false;
            this.galleryid = galleryid;
            this.file = filestream;
            this.filename = filename;
            this.siteid = siteid;
        }

        private Guid siteid;
        private Stream file;
        private string filename;
        private string url;
        private int galleryid;
        private bool remote;
        public event UploadedAttachment UploadCompleted;

        public void UploadAttachment()
        {
            try
            {
                MediaGalleriesService mgs = Sites.Instance[this.siteid].Service;
                if (remote) UploadCompleted(mgs.AddTemporaryAttachment(this.galleryid, this.url));
                else UploadCompleted(mgs.AddTemporaryAttachment(this.galleryid, this.filename, this.file));
            }
            catch (Exception e) { MessageBox.Show("Connection Error:\n" + e.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }

    public delegate void UploadedAttachment(Guid guid);
}
